package com.zretc.support.dao;

import com.zretc.support.entity.User;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.List;

public interface UserDao extends JpaRepository<User,String>, JpaSpecificationExecutor<User>,
        CrudRepository<User,String>, Serializable {
    //验证用户名和密码
    @Transactional
    User findByUserTelAndUserPwdAndUserRole(String userName,String userPwd,int userRole);

    //分页查询+分页(用户名查)
    @Query("select u from User u where u.userName like ?1% and u.userRole = ?2")
    @Transactional
    Page<User>findByLike(String username,int userRole, Pageable pageable);

    //分页查询+分页(电话查)
    @Query("select u from User u where u.userTel = ?1 and u.userRole = ?2")
    @Transactional
    Page<User>findByTel(String tel,int userRole, Pageable pageable);

    //通过id查询用户
    @Transactional
    User findByUserId(String userId);

    //下拉框分页查询+分页（用户名查询）
    @Query("select u from User u where u.userName like ?1% and u.userRole = ?2 and u.userSex = ?3 ")
    @Transactional
    Page<User>findBySelectU(String username,int userRole, String sex ,Pageable pageable);

    //下拉框分页查询+分页（通过电话号码查询）
    @Query("select u from User u where u.userTel = ?1 and u.userRole = ?2 and u.userSex = ?3  ")
    @Transactional
    Page<User>findBySelectT(String tel,int userRole, String sex , Pageable pageable);

    List<User> findAllByUserRole(int userRole);

    User findByUserTel(String userTel);

}
